package com.bodacn.keypoint;

import android.view.KeyEvent;

public class ScannerManager {
    private Boolean mCaps = false;
    String codeStr = "";
    OnScanListener listener;
    boolean isInterrupt = true;
    public ScannerManager(OnScanListener listener) {
        this.listener = listener;
    }

    /**
     * 处理输入事件
     *
     * @param event
     * @return true 表示消费掉，拦截不在传递， false 不管
     */

    public boolean dispatchKeyEvent(KeyEvent event) {
        System.out.println(event);
        /**
         * 系统的软键盘  按下去是 -1, 不管，不拦截
         */
        if (event.getDeviceId() == -1) {
            return false;
        }
        int keyCode = event.getKeyCode();
        checkLetterStatus(event);//检查是否有大小写Shift；
        //按下弹起，识别到弹起的话算一次 有效输入
        //只要是 扫码枪的事件  都要把他消费掉 不然会被editText 显示出来
        if (event.getAction()== KeyEvent.ACTION_MULTIPLE){
            codeStr=event.getCharacters();
            if (listener != null&&codeStr.length()>0) {
                listener.onResult(codeStr);
                codeStr = "";
            }
        }
        if (event.getAction() == KeyEvent.ACTION_UP) {
            //识别到结束，当下使用的设备是  是还会有个KEYCODE_DPAD_DOWN 事件，不知道其它设备有没有  先忽略
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                if (listener != null&&codeStr.length()>0) {
                    listener.onResult(codeStr.toUpperCase());
                    codeStr = "";
                }
            } else{
                char ch = getInputCode(event);
                if (ch!=0) codeStr+=ch;
            }

        }
        //都是扫码枪来的事件，选择消费掉
        return isInterrupt;
    }

    private char getInputCode(KeyEvent event) {

        int keyCode = event.getKeyCode();
        char aChar = 0;

        if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
            //字母
            aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
        } else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
            //数字
            aChar = caseNumber(keyCode);
        } else {
            //其他符号
            switch (keyCode) {
                case KeyEvent.KEYCODE_PERIOD:
                    aChar = mCaps?'>':'.';
                    break;
                case KeyEvent.KEYCODE_MINUS:
                    aChar = mCaps ? '_' : '-';
                    break;
                case KeyEvent.KEYCODE_SLASH:
                    aChar = mCaps?'?':'/';
                    break;
                case KeyEvent.KEYCODE_BACKSLASH:
                    aChar = mCaps ? '|' : '\\';
                    break;
                case KeyEvent.KEYCODE_NUMPAD_MULTIPLY:
                    aChar = '*';
                    break;
                case KeyEvent.KEYCODE_ENTER:
                    aChar = '\n';
                    break;
                case KeyEvent.KEYCODE_EQUALS:
                    aChar = mCaps?'+':'=';
                    break;
                default:
                    break;
            }
        }

        return aChar;
    }

    private char caseNumber(int keyCode){
        char ch = (char) ('0' + keyCode - KeyEvent.KEYCODE_0);
        if(!mCaps){
            return ch;
        }
        switch (keyCode){
            case KeyEvent.KEYCODE_0:return ')';
            case KeyEvent.KEYCODE_1:return '!';
            case KeyEvent.KEYCODE_2:return '@';
            case KeyEvent.KEYCODE_3:return '#';
            case KeyEvent.KEYCODE_4:return '$';
            case KeyEvent.KEYCODE_5:return '%';
            case KeyEvent.KEYCODE_6:return '^';
            case KeyEvent.KEYCODE_7:return '&';
            case KeyEvent.KEYCODE_8:return '*';
            case KeyEvent.KEYCODE_9:return '(';
        }

        return ch;
    }
    private void checkLetterStatus(KeyEvent event) {
        int keyCode = event.getKeyCode();
        if (keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT || keyCode == KeyEvent.KEYCODE_SHIFT_LEFT) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                //按着shift键，表示大写
                mCaps = true;
            } else {
                //松开shift键，表示小写
                mCaps = false;
            }
        }
    }
    public interface OnScanListener {

        void onResult(String code);
    }

    public void setInterrupt(boolean interrupt) {
        isInterrupt = interrupt;
    }
}
